package cn.xsirl.websocket.util;

import java.io.*;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {
    public static String FILE_LOCATION = System.getProperty("user.dir");
    /**
     * 文件保存目录
     */
    public static String DIR = "upload";
    public static Map<String, String> MAPPING = new HashMap<>();
    
    static {
        MAPPING.put("jpg,jpeg,png,gif", "image");
        MAPPING.put("mp4,avi,wav", "video");
    }

    public void save(String fileName, String fileType, byte[] bytes) {
        File file = Paths.get(FILE_LOCATION)
                .resolve(DIR)
                .resolve(fileType.split("/")[0])
                .toFile();
        
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
    
        try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
            out.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
}
